<!doctype html>
<html class="no-js" lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta charset="utf-8">

    <title>Utbudstjänst - ID-mappning</title>

    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">

    <link rel="stylesheet" href="../css/font-awesome.css">
    <link rel="stylesheet" href="../css/bootstrap.css">
    <link rel="stylesheet" href="../css/prism.css">
    <link rel="stylesheet" href="../css/chosen.min.css">
    <link rel="stylesheet" href="../css/bootstrap-responsive.css">
    <link rel="stylesheet" href="../css/datepicker.css">
    <link rel="stylesheet" href="../css/tablesorter.css">
    <link rel="stylesheet" href="../css/ui-lightness/jquery-ui-1.8.21.custom.css">
    <link rel="stylesheet" href="../css/application-ocean-breeze.css">
    <link rel="stylesheet" href="../css/msgGrowl.css" type="text/css" charset="utf-8" />
    <link rel="stylesheet" href="../css/jquery.msgbox.css">
    <link rel="stylesheet" href="../css/jquery.dataTables.css">
    <script src="../js/lib/modernizr-2.5.3.min.js"></script>
</head>

<body>
<script id="detailTemplate" type="text/html">
    <h2 class="closeable unmodified"><i class="icon-check-minus"></i> <span id="edit-title">ID-mappning</span></h2>

    <div class="row-fluid unmodified">
        <div class="alert">
            <strong>Du har osparade ändringar!</strong> För att stänga raden måste du <a class="action-save">spara</a> eller <a class="action-cancel">avbryta</a>.
        </div>
    </div>

    <div id="edit-idmap">
        <div class="row-fluid">
            <div class="span8">
                <input id="vmId" type="hidden" />
                <input id="vmCareGiverPartId" type="hidden" />

                <div class="control-group" id="vm-unitName">
                    <label>Enhetsnamn (Från HSA)</label>
                    <div class="controls">
                        <input id="hsaRelativeDistinguishedName" class="input-xxlarge" type="text" disabled="true"/>
                    </div>
                </div>

                <div class="control-group" id="vm-careGiverName">
                    <label>Vårdgivarnamn (Från Vårdmarknad)</label>
                    <div class="controls">
                        <input id="vmCareGiverName" class="input-xxlarge" type="text" disabled="true"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span8">
                <legend>ID mappning</legend>
                <div class="control-group" id="vm-reg">
                    <label>Regionalt HSA-ID</label>
                    <div class="controls">
                        <input id="ekId" class="input-xxlarge" type="text" />
                    </div>
                </div>

                <div class="control-group" id="vm-nat">
                    <label>Nationellt HSA-ID</label>
                    <div class="controls">
                        <input id="hsaId" class="input-xxlarge" type="text" />
                    </div>
                </div>

                <div class="control-group" id="vm-sklId">
                    <label>SKL-ID</label>
                    <div class="controls">
                        <input id="sklId" class="input-xxlarge" type="text" />
                    </div>
                </div>
            </div>

            <div class="span4">
                <legend>Import/Export</legend>

                <div class="control-group">
                    <label>Import från Vårdmarknad</label>
                    <select id="allowImport" class="chosen-select listen-change">
                        <option value="true">Ja</option>
                        <option value="false">Nej</option>
                    </select>
                </div>

                <div class="control-group">
                    <label>Export till SKL</label>
                    <select id="allowExport" class="chosen-select listen-change">
                        <option value="true">Ja</option>
                        <option value="false">Nej</option>
                    </select>
                </div>
            </div>
        </div>

        <!-- Actions Save & Cancel -->
        <div class="row-fluid actions-item">
            <div class="span12 unmodified">
                <div class="pull-right">
                    <a class="btn action-cancel"><i class="icon-remove"></i> Avbryt</a>
                    <a class="btn btn-primary action-save" disabled="disabled"><i class="icon-thumbs-up"></i> Spara</a>
                </div>
            </div>
        </div>

    </div>
</script>
<div id="wrapper">
    <div id="headerPlaceholder"></div>
    <div id="content">
        <div class="container">
            <table id ="idmapTable" class="table table-hover table-striped">
                <thead>
                <tr>
                    <th width="17%">Enhetsnamn (HSA)</th>
                    <th width="17%">Regionalt HSA-ID</th>
                    <th width="17%">Nationellt HSA-ID</th>
                    <th width="17%">Kombika ID</th>
                    <th width="17%">SKL ID</th>
                    <th width="17%">Vårdgivarnamn</th>
                    <th width="13%">Import</th>
                    <th width="13%">Export</th>
                </tr>
                </thead>
                <tbody id ="idmapBody">
                </tbody>
            </table>
        </div> <!-- /.container -->
    </div> <!-- /#content -->
</div> <!-- /#wrapper -->

<script src="../js/lib/jquery-1.7.2.min.js"></script>
<script src="../js/lib/jquery-ui-1.8.21.custom.min.js"></script>
<script src="../js/lib/jquery.ui.touch-punch.min.js"></script>
<script src="../js/lib/bootstrap.min.js"></script>
<script src="../js/lib/moment.min.js"></script>
<script src="../js/lib/jquery.tablesorter.min.js"></script>
<script src="../js/lib/jquery.dataTables.js"></script>
<script src="../js/lib/jquery.validate.js"></script>
<script src="../js/lib/chosen.jquery.min.js"></script>
<script src="../js/lib/msgGrowl.min.js"></script>
<script src="../js/lib/jquery.cookie.js"></script>
<script src="../js/lib/jquery-handlebars-1.1.4.min.js"></script>
<script src="../js/md.js"></script>
<script src="../js/common.js"></script>
<script>
            var idmapGet = config.guiURL + '/rest/aggregated/idmap';
            var idmapPut = config.idmapURL;
            commonInit({
                tabIdmap: true,
                viewDataUrl: config.guiURL + '/view/idmap',
                mastheadTitle: 'ID-mappning',
                mastheadSubtitle: 'Mappning av ID:n hos olika vårdgivare.',
                mastheadContent: ''
            });

            $("#idmapTable").MasterDetail({
                masterData: viewData.idmap,
                masterTableId: 'idmapTable',
                masterColumns: [
                    {
                        mData: 'hsaRelativeDistinguishedName',
                        mRender: MasterDetail.renderString
                    }, {
                        mData: 'ekId',
                        mRender: MasterDetail.renderString
                    }, {
                        mData: 'hsaId',
                        mRender: MasterDetail.renderString
                    }, {
                        mData: 'kombika',
                        mRender: MasterDetail.renderString
                    }, {
                        mData: 'sklId',
                        mRender: MasterDetail.renderString
                    }, {
                        mData: 'vmCareGiverName',
                        mRender: MasterDetail.renderString
                    }, {
                        mData: 'allowImport',
                        mRender: MasterDetail.renderYesNo
                    }, {
                        mData: 'allowExport',
                        mRender: MasterDetail.renderYesNo
                    }
                ],
                initDetail: initDetail,
                updateDetail: updateDetail,
                validateDetail: validateDetail
            });

            function initDetail(container, data) {
                var vardgivarnamn = data.hsaRelativeDistinguishedName || data.vmCareGiverName;
                $('#vmId', container).val(data.vmId);
                $('#vmCareGiverPartId', container).val(data.vmCareGiverPartId);
                $('#vmCareGiverName', container).val(data.vmCareGiverName).prop('disabled', true);
                $('#vmCareGiverPartName', container).val(data.vmCareGiverPartName).prop('disabled', true);
                $('#hsaRelativeDistinguishedName:', container).val(data.hsaRelativeDistinguishedName).prop('disabled', true);
                $('#hsaId', container).val(data.hsaId);
                $('#ekId', container).val(data.ekId);
                $('#sklId', container).val(data.sklId);
                $('#allowImport', container).val(data.allowImport.toString());
                $('#allowExport', container).val(data.allowExport.toString());
                $('#edit-title', container).text(vardgivarnamn);

                $("#allowImport").chosen({
                    disable_search: true
                });
                $("#allowExport").chosen({
                    disable_search: true
                });

            }

            function updateDetail(container) {
                console.log(container);
                data = {
                    vmId: $('#vmId', container).val(),
                    vmCareGiverPartId: $('#vmCareGiverPartId', container).val(),
                    vmCareGiverName: $('#vmCareGiverName', container).val(),
                    vmCareGiverPartName: $('#vmCareGiverPartName', container).val(),
                    hsaRelativeDistinguishedName: $('#hsaRelativeDistinguishedName:', container).val(),
                    hsaId: $('#hsaId', container).val(),
                    ekId: $('#ekId', container).val(),
                    kombika: $('#kombika', container).val(),
                    sklId: $('#sklId', container).val(),
                    allowExport: $('#allowExport', container).val(),
                    allowImport: $('#allowImport', container).val()
                };
                console.log(data);

                var retData = null;
                $.ajax({
                    url: idmapPut,
                    type: 'PUT',
                    data: JSON.stringify(data),
                    contentType: 'application/json; charset=UTF-8',
                    dataType: 'json',
                    async: false,
                    success: function(data) {
                        showGrowlMessage('ID-map uppdaterad.');
                        retData = data;
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        showErrorGrowlMessage(jqXHR.responseText);
                        if (textStatus==='error' && errorThrown==='Unauthorized') {
                            alert("Din användarsession har gått ut, du måste logga in på nytt!")
                            $(location).attr('href','../');
                        }
                    }
                });
                return retData;
            }

            /**
            * Validates form
            * 
            * @param {type} container
            * @returns {true or false}
            */
            function validateDetail(container) {
                return true;
            }

        </script>
</body>
</html>
